using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// 近战攻击策略实现
/// </summary>
public class MeleeAttackStrategy : AttackStrategyBase
{
    private float m_Interval;
    private float m_Radius;
    private float m_Damage;

    public MeleeAttackStrategy(SkillData data)
    {
        AttackData = data;
        if (data != null)
        {
            m_Interval = data.Interval;
            m_Radius = data.Range;
            m_Damage = data.Damage;
        }
    }

    public MeleeAttackStrategy(float interval, float radius, float damage, IAttackEffect effect)
    {
        m_Interval = interval;
        m_Radius = radius;
        m_Damage = damage;
    }

    public override void Execute(Entity target)
    {
        base.Execute(target);

        if (target == null || target.HealthComponent == null)
        {
            Logger.LogWarning("MeleeAttackStrategy", $"{Owner?.name} 攻击目标无效");
            return;
        }

        // 检查距离
        if (Owner != null && target != null)
        {
            float distance = Vector3.Distance(Owner.transform.position, target.transform.position);
            if (distance > m_Radius)
            {
                Logger.LogWarning("MeleeAttackStrategy", $"{Owner.name} 目标 {target.name} 超出攻击范围 {distance} > {m_Radius}");
                return;
            }
        }

        // 如果技能数据有Effects列表，使用SkillExecutor执行效果
        if (AttackData != null && AttackData.Effects != null && AttackData.Effects.Count > 0)
        {
            SkillExecutor.Execute(AttackData.Effects, Owner, target);
            Logger.Log("MeleeAttackStrategy", $"{Owner?.name} 通过效果列表近战攻击 {target.name}");
        }
        else
        {
            // 如果没有效果列表，直接造成伤害
            long damage = (long)m_Damage;
            if (AttackData != null)
            {
                damage = AttackData.Damage;
            }
            
            target.HealthComponent.TakeDamage(damage);
            Logger.Log("MeleeAttackStrategy", $"{Owner?.name} 对 {target.name} 造成 {damage} 点近战伤害");
        }
    }
}
